
import { injectable } from 'inversify';
import * as path from 'path';
import { ConfigurationChangeEvent, Event, Uri, workspace, WorkspaceConfiguration, WorkspaceFolder, WorkspaceFoldersChangeEvent } from 'vscode';
import { Resource } from '../types';
import { getOSType, OSType } from '../utils/platform';
import { IWorkspaceService } from './types';
@injectable()
export class WorkspaceService implements IWorkspaceService {
    public getWorkspaceFolderIdentifier(resource: Resource, defaultValue: string = ''): string {
        const workspaceFolder = resource ? workspace.getWorkspaceFolder(resource) : undefined;
        return workspaceFolder
            ? path.normalize(
                getOSType() === OSType.Windows
                    ? workspaceFolder.uri.fsPath.toUpperCase()
                    : workspaceFolder.uri.fsPath,
            )
            : defaultValue;
    }
    public getWorkspaceFolder(uri: Resource): WorkspaceFolder | undefined {
        return uri ? workspace.getWorkspaceFolder(uri) : undefined;
    }
    public get onDidChangeConfiguration(): Event<ConfigurationChangeEvent> {
        return workspace.onDidChangeConfiguration;
    }
    public getConfiguration(section?: string, resource?: Uri): WorkspaceConfiguration {
        return workspace.getConfiguration(section, resource || null);
    }

    public get onDidChangeWorkspaceFolders(): Event<WorkspaceFoldersChangeEvent> {
        return workspace.onDidChangeWorkspaceFolders;
    }
    public get workspaceFolders(): readonly WorkspaceFolder[] | undefined {
        return workspace.workspaceFolders;
    }
    public get onDidGrantWorkspaceTrust(): Event<void> {
        return workspace.onDidGrantWorkspaceTrust;
    }

    public get isVirtualWorkspace(): boolean {
        const isVirtualWorkspace =
            workspace.workspaceFolders && workspace.workspaceFolders.every((f) => f.uri.scheme !== 'file');
        return !!isVirtualWorkspace;
    }

    public get isTrusted(): boolean {
        return workspace.isTrusted;
    }

}